package com.nerve.android.cloudoffice.app.activity.task;

import java.util.ArrayList;
import java.util.List;

import org.nerve.android.NerveActivityGroup;
import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;
import org.nerve.util.DateUtil;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.TrashApply;
import com.nerve.android.cloudoffice.service.data.TaskData;

@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_list_content)
public class TrashApplyActivity extends NavigationActivity{

	private static final int LIST_LOAD = 0;
	private static final int LIST_OK = 2;
	
	
	@ViewOnId(id=R.id.listContent)
	private ListView listView;
	@ViewOnId(id=R.id.message_empty)
	private TextView messageTV;
	
	private int id;
	private Button lastClickBtn;
	
	private ApplyAdapter adapter;
	List<TrashApply> applyList = new ArrayList<TrashApply>();
	
	private OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v instanceof Button){
				lastClickBtn = (Button)v;
				Integer index = Integer.valueOf(v.getTag().toString());
				id = index;
				
				showProgressDialog(null, "正在操作，请稍候...");
				new TrashWorker().execute(-1);
			}
		}
	};
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			
			case LIST_OK:
				if(adapter != null)
					adapter.notifyDataSetChanged();
				break;
				
			case WORKER_OK:
				hideProgressDialog();
				lastClickBtn.setEnabled(false);
				break;
			}
		}
	};
	
	@Override
	protected void initUI() {
		super.initUI();
		
		messageTV.setText("没有任何删除申请");
		listView.setEmptyView(messageTV);
		
		adapter = new ApplyAdapter();
		listView.setAdapter(adapter);
		
		setNavTitle("任务删除申请");
	}
	
	@Override
	protected void onNavigateBackButtonClick() {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		
		group.changeTab(IndexGroupActivity.HOME);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		new TrashWorker().execute(LIST_LOAD);
	}
	
	@Override
	protected void initData() {
	}
	
	private class ApplyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return applyList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return applyList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			TrashApply ta = (TrashApply)getItem(arg0);
			if(arg1 == null)
				arg1 = View.inflate(TrashApplyActivity.this, R.layout.activity_trashapply_listitem, null);
			
			((TextView)arg1.findViewById(R.id.task)).setText("任务标题: " + ta.getTask());
			((TextView)arg1.findViewById(R.id.sender)).setText("申请员工: " + ta.getSender());
			((TextView)arg1.findViewById(R.id.passer)).setText("审批员工: " + ta.getPasser());
			((TextView)arg1.findViewById(R.id.date)).setText(
					"申请日期: " + DateUtil.toString(ta.getJoinDate(), "yyyy-MM-dd")
					);
			
			Button btn = (Button)arg1.findViewById(R.id.okBtn);
			Const.log("BTN:" + btn);
			btn.setOnClickListener(clickListener);
			btn.setTag(ta.getId());
			if(ta.isAgree()){
				btn.setEnabled(false);
			}
			return arg1;
		}
	}
	
	private class TrashWorker extends AsyncTask<Integer, Void, Integer>{

		@SuppressWarnings("unchecked")
		@Override
		protected Integer doInBackground(Integer... params) {
			if(params[0] == LIST_LOAD){
				Reply re = TaskData.getTrashApply();
				
				if(re.isError()){
					Const.ERROR = re.getMessage().toString();
					return WORKER_FAIL;
				}
				
				applyList = (List<TrashApply>)re.getMessage();
				return LIST_OK;
			}
			else{
				Reply re = TaskData.passApply(id);
				
				if(re.isError()){
					Const.ERROR = re.getMessage().toString();
					return WORKER_FAIL;
				}
				
				return WORKER_OK;
			}
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}
}